package entity;

import java.io.Serializable;
import java.util.Collection;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

@Entity
@Table(name = "account_group")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "AccountGroup.findAll", query = "SELECT a FROM AccountGroup a"),
    @NamedQuery(name = "AccountGroup.findById", query = "SELECT a FROM AccountGroup a WHERE a.id = :id"),
    @NamedQuery(name = "AccountGroup.findByName", query = "SELECT a FROM AccountGroup a WHERE a.name = :name")})
public class AccountGroup implements Serializable {
    private static final long serialVersionUID = 1L;
    private Integer id;
    private String name;
    private Collection<Account> accounts;

    public AccountGroup() {
    }

    public AccountGroup(Integer id) {
        this.id = id;
    }

    public AccountGroup(Integer id, String name) {
        this.id = id;
        this.name = name;
    }
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id")
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 45)
    @Column(name = "name")
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @XmlTransient
    @JoinTable(name = "account_is_in_group", joinColumns = {
        @JoinColumn(name = "account_group_id", referencedColumnName = "id")}, inverseJoinColumns = {
        @JoinColumn(name = "account_id", referencedColumnName = "id")})
    @ManyToMany    
    public Collection<Account> getAccounts() {
        return accounts;
    }

    public void setAccounts(Collection<Account> accounts) {
        this.accounts = accounts;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        if (!(object instanceof AccountGroup)) {
            return false;
        }
        AccountGroup other = (AccountGroup) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return name;
    }
    
}
